Updated to latest API version and made some refactorings

Hey Aleksandar, your code gave me a starting point. I decided to update it to latest API version and made some refactorings. Please feel free to merge it in.