Cleaned up code, simplified functions