some serious bug fixing