cleanup code and simplify