Refactors and prepares for exception handling