some working code

It's not the best quality code, but I had it checked with JSLint and it kinda works...