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