Moved functionality into functions and classes