refactored to use prototypes also added heavy documentation part 2