Made changes suggested by asmeurer. Tests yet to be added