code refactor, better exception handling,