simplify and cleanup code