Finally tested and debugged :)