copied parts of the code to save time