working version

working version, needs more comments