Some optimzations and improvments