Code clean up and manually tested