Completely rewrote class again, now more secure and optimized.