Add memory detection and some helper functions