tidy and made it work
better working version