optimize some code. Incr tests coverage