cleaned up and structured code a little