Fixed issues with first try at cleaning up code