Some minor code style fixes
Move some functions about
Remove some dead code