added handling of weight (may still need some fine tuning