Split into more functions
General improvements