Moved some things around for better code and added function headers.