tidy'd up a bit... added more comments... starting on the calculations. havent started on api at this point