This is not optimized. In fact, half of it is improvised. Added for more leaderboard completeness though.