rearrange code and remove unnecessary returns