Bulk of the work done, some unit tests added too