Added most of the code