Most of logic added