Moved some function to the private methods section, and fixed some bugs