Reimplemented some commits that I accidently reverted previously