refactoring. and make more helper functions. more exceptions and less gils