Most of the code