cleaned up code with functions