moved some code to separate functions