Lots of changes to api and pg, better object wrapping, error handling