Optimilizing code, so that GUI thread is not doing so much computing