Changed the implementation a little bit, please take another look.