Compressed the if-block into a loop
Added test for 500