Merged two more solutions
Little refacotring for clarity