Some sanity checks & refactoring