added copyright notice, refactored the code