Added documentation and changed some statements to keep make the code DRY