refactored to use prototypes also added heavy documentation