moving some things around, better error handling