Cleaning up memory implementation; fixing size miscalculations