split some code into smaller pieces and added a few functions