Significant bug fixing