Code cleanup, modify internal functions and minimised layout