removed debugging code
refactored code a bit and fix php warnings