cleaned up code, made things slightly faster