Cleaned up and fixed some functions