#6 Some improvements and code optimization