Improved the code by abstraction