Build improvements and Error handling

Adds error handling and allows the state field to be used more generally.