Various fixes and rewrite the missing parts