first code
fixed minor problems
first working copy