added more documentation, made code more readable and logical