improved solution for 599 (0.266s -> 0.232s)