1. further simplifies the error checking
2. change weight.function to weight.function