Reorganized code for better and easier re-usability