Added license header and some code optimized.