fixed testcode bug and rupbgsave implementation