Various improvements and fix unreachable code