Moved some functions out to seperate file
Tightened up.