moved two proccesses into functions for debugging and readability