Some bug fixing and polishing