Simplify code and make it more efficient