Simplifying/refactoring code