debugs and small refactors, better error messaging