Refactor application to get rid of global functions