cleaned up code to be more elegant