Refined code, reduced extra space useage