first version with all necessary functions implemented; it compiles but is untested
added weight averaging