moved code & cleaned up a bit