separated/cleaned up code a little